class Question {
  final int? id;
  final String question;
  String? questionImageUrl; // 题目图片路径
  final String optionA;
  String? optionAImageUrl; // 选项A图片路径
  final String optionB;
  String? optionBImageUrl; // 选项B图片路径
  final String optionC;
  String? optionCImageUrl; // 选项C图片路径
  final String optionD;
  String? optionDImageUrl; // 选项D图片路径
  final String correctAnswer;
  final String explanation;
  String? explanationImageUrl; // 解析图片路径
  final String category;
  final String keyPoint; // 添加考点字段
  final int difficulty;
  final String tags;
  final int created_at;
  final int updated_at;
  final String type; // 添加题目类型字段

  Question({
    this.id,
    required this.question,
    this.questionImageUrl,
    required this.optionA,
    this.optionAImageUrl,
    required this.optionB,
    this.optionBImageUrl,
    required this.optionC,
    this.optionCImageUrl,
    required this.optionD,
    this.optionDImageUrl,
    required this.correctAnswer,
    this.explanation = '',
    this.explanationImageUrl,
    this.category = '',
    this.keyPoint = '', // 初始化考点字段
    this.difficulty = 1,
    this.tags = '',
    required this.created_at,
    required this.updated_at,
    required this.type, // 不设置默认值，必须提供
  });

  // 从Map创建Question对象
  factory Question.fromMap(Map<String, dynamic> map) {
    return Question(
      id: map['id']?.toInt(),
      question: map['question'] ?? '',
      questionImageUrl: map['question_image_url'],
      optionA: map['option_a'] ?? '',
      optionAImageUrl: map['option_a_image_url'],
      optionB: map['option_b'] ?? '',
      optionBImageUrl: map['option_b_image_url'],
      optionC: map['option_c'] ?? '',
      optionCImageUrl: map['option_c_image_url'],
      optionD: map['option_d'] ?? '',
      optionDImageUrl: map['option_d_image_url'],
      correctAnswer: map['correct_answer'] ?? '',
      explanation: map['explanation'] ?? '',
      explanationImageUrl: map['explanation_image_url'],
      category: map['category'] ?? '',
      keyPoint: map['key_point'] ?? '', // 从Map中获取考点字段
      difficulty: map['difficulty']?.toInt() ?? 1,
      tags: map['tags'] ?? '',
      created_at: map['created_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      updated_at: map['updated_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      type: map['type'] ?? '', // 从Map中获取题目类型字段，不设置默认值
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'question': question,
      'question_image_url': questionImageUrl,
      'option_a': optionA,
      'option_a_image_url': optionAImageUrl,
      'option_b': optionB,
      'option_b_image_url': optionBImageUrl,
      'option_c': optionC,
      'option_c_image_url': optionCImageUrl,
      'option_d': optionD,
      'option_d_image_url': optionDImageUrl,
      'correct_answer': correctAnswer,
      'explanation': explanation,
      'explanation_image_url': explanationImageUrl,
      'category': category,
      'key_point': keyPoint, // 将考点字段添加到Map
      'difficulty': difficulty,
      'tags': tags,
      'created_at': created_at,
      'updated_at': updated_at,
      'type': type, // 将题目类型字段添加到Map
    };
  }

  // 复制并修改
  Question copyWith({
    int? id,
    String? question,
    String? questionImageUrl,
    String? optionA,
    String? optionAImageUrl,
    String? optionB,
    String? optionBImageUrl,
    String? optionC,
    String? optionCImageUrl,
    String? optionD,
    String? optionDImageUrl,
    String? correctAnswer,
    String? explanation,
    String? explanationImageUrl,
    String? category,
    String? keyPoint, // 添加考点字段
    int? difficulty,
    String? tags,
    int? created_at,
    int? updated_at,
    String? type, // 添加题目类型字段
  }) {
    return Question(
      id: id ?? this.id,
      question: question ?? this.question,
      questionImageUrl: questionImageUrl ?? this.questionImageUrl,
      optionA: optionA ?? this.optionA,
      optionAImageUrl: optionAImageUrl ?? this.optionAImageUrl,
      optionB: optionB ?? this.optionB,
      optionBImageUrl: optionBImageUrl ?? this.optionBImageUrl,
      optionC: optionC ?? this.optionC,
      optionCImageUrl: optionCImageUrl ?? this.optionCImageUrl,
      optionD: optionD ?? this.optionD,
      optionDImageUrl: optionDImageUrl ?? this.optionDImageUrl,
      correctAnswer: correctAnswer ?? this.correctAnswer,
      explanation: explanation ?? this.explanation,
      explanationImageUrl: explanationImageUrl ?? this.explanationImageUrl,
      category: category ?? this.category,
      keyPoint: keyPoint ?? this.keyPoint, // 设置考点字段
      difficulty: difficulty ?? this.difficulty,
      tags: tags ?? this.tags,
      created_at: created_at ?? this.created_at,
      updated_at: updated_at ?? this.updated_at,
      type: type ?? this.type, // 设置题目类型字段
    );
  }

  @override
  String toString() {
    return 'Question{id: $id, type: $type, question: $question, category: $category}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Question && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}